package com.cn.hnust.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.hnust.pojo.Fenxiang;
import com.cn.hnust.service.IFenxiangService;
import com.google.gson.Gson;

@Controller
@RequestMapping("/Fenxiang")
public class FenxiangController {

	@Resource
	private IFenxiangService FenxiangService;

	@RequestMapping("/selectFenxiangByOpenid")
	public void selectFenxiangByOpenid(HttpServletResponse response,Fenxiang Fenxiang,String openid) throws IOException {
		List<Fenxiang> list = FenxiangService.selectFenxiangByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}
	
	@RequestMapping("/addFenxiang")
	public void addFenxiang(HttpServletResponse response,Fenxiang Fenxiang,String openid,String  img,String name,Integer fid) throws IOException {
		Fenxiang.setFid(fid);
		Fenxiang.setImg(img);
		Fenxiang.setName(name);
		Fenxiang.setOpenid(openid);
		FenxiangService.insertSelective(Fenxiang);
		List<Fenxiang> list = FenxiangService.selectFenxiangByOpenid(openid);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		response.setContentType("text/json,charset=utf-8");
		response.setCharacterEncoding("utf-8");
		Writer out = response.getWriter();
		out.write(json);
		out.flush();
	}

}
